package com.jokul.web.user;

import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 *@author jokul
 *@2013-3-31 
 *@下午8:44:31
 */
@MultipartConfig
@WebServlet(urlPatterns = "/forget_password.do", initParams = {
		@WebInitParam(name = "mailHost", value = "smtp.gmail.com"),
		@WebInitParam(name = "mailPort", value = "465"),
		@WebInitParam(name = "username", value = "jokul2011@gmail.com"),
		@WebInitParam(name = "password", value = "jokul5658551") })
public class ForgetPassword extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	private String mailHost;
	private String mailPort;
	private String username;
	private String password;
	private Properties properties;

	public void init() throws ServletException {
		mailHost = getServletConfig().getInitParameter("mailHost");
		mailPort = getServletConfig().getInitParameter("mailPort");
		username = getServletConfig().getInitParameter("username");
		password = getServletConfig().getInitParameter("password");

		properties = new Properties();
		properties.put("mail.smtp.host", mailHost);
		properties.setProperty("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		properties.setProperty("mail.smtp.socketFactory.fallback", "false");
		properties.setProperty("mail.smtp.port", mailPort);
		properties.setProperty("mail.smtp.socketFactory.port", mailPort);
		properties.setProperty("mail.smtp.auth", "true");
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		String from = username;
		String to = request.getParameter("email");
		String subject = "密码";
		String text = "你的密码是000000";

		try {
			Message message = getMessage(from, to, subject, text);
			Transport.send(message);
			response.getWriter().println("发送成功！");
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}

	}

	private Message getMessage(String from, String to, String subject,
			String text) throws AddressException, MessagingException, IOException {
		Session session = Session.getDefaultInstance(properties,
				new Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				});
		Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress(from));
		message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
		message.setSubject(subject);
		message.setSentDate(new Date());
		message.setText(text);

		return message;
	}

}
